package org.xjtu.weteam.common.utils;

import org.xjtu.weteam.model.domain.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import jakarta.servlet.http.HttpSession;


/**
 * @author Kevin
 */
public class SessionUtils {

    private static final String USERKEY = "sessionUser";

    public static HttpSession session() {
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        return attr.getRequest().getSession(true);
    }

    // 保存用户到 session
    public static void saveCurrentUser(User user) {
        session().setAttribute(USERKEY, user);
    }

    // 从 session 获取用户
    public static User getCurrentUser() {
        return (User) session().getAttribute(USERKEY);
    }

    // 清除 session 中的用户
    public static void clearCurrentUser() {
        session().removeAttribute(USERKEY);
    }
}
